#include "LevelEditor.h"

#include <gl\glew.h>
#include <gl\glfw.h>
#include "shapes3d.h"
#include <algorithm>
using namespace Shapes3D;
using std::min;
using std::max;

LevelEditor* LevelEditor::_instance = 0x0;

LevelEditor::LevelEditor()
{
	_instance = 0x0;
	startedRect = false;
}

void LevelEditor::saveToFile()
{

}

void LevelEditor::loadFromFile()
{

}

void LevelEditor::setCursorPos( glm::vec3 &pos )
{
	cursorPos = pos;
}

void LevelEditor::createNavRect( glm::vec3 &pos )
{
	if( !startedRect )
	{
		startedRect = true;
		startPoint = pos;
	}else
	{
		printf("made nav rect\n");
		startedRect = false;
		navRectList.push_back( Rect(startPoint, pos) );
	}

}

LevelEditor* LevelEditor::Instance()
{
	if( !_instance )
	{
		_instance = new LevelEditor();
	}

	return _instance;
}

void LevelEditor::render()
{
	glColor3ub(255,255,0);

	glUseProgram(0);

	for(size_t i=0; i<navRectList.size(); i++){
		glPushMatrix();

		glTranslatef( navRectList[i].getCenter()[0], navRectList[i].getCenter()[1] + .1f , navRectList[i].getCenter()[2] );

		DrawWireGrid( navRectList[i].getWidth(),  navRectList[i].getLength(), max<float>(10, navRectList[i].getWidth() + navRectList[i].getLength() ) );

		glPopMatrix();
	}


	// Currently making navrect
	if( startedRect )
	{
		Rect activeRect( startPoint, cursorPos );

		glPushMatrix();

		glTranslatef( activeRect.getCenter()[0], activeRect.getCenter()[1] + .1f , activeRect.getCenter()[2] );

		DrawWireGrid( activeRect.getWidth(),  activeRect.getLength(), max<float>(10, activeRect.getWidth()+activeRect.getLength() )  );

		glPopMatrix();
	}

}
